package com.lfy.kcat.workflow.biz;

import com.lfy.kcat.workflow.ai.OllamaModerationService;
import com.lfy.kcat.workflow.feign.ContentServiceFeignClient;
import lombok.extern.slf4j.Slf4j;
import org.camunda.bpm.engine.TaskService;
import org.camunda.bpm.engine.delegate.DelegateExecution;
import org.camunda.bpm.engine.task.Task;
import org.dromara.common.core.dto.DramaAuthCompleteDTO;
import org.dromara.common.core.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * @author leifengyang
 * @version 1.0
 * @date 2025/9/12 15:23
 * @description:
 */
@Slf4j
@Service
public class CamundaJavaDelegateHandler {
    @Autowired
    TaskService taskService;


    @Autowired
    ContentServiceFeignClient contentServiceFeignClient;


    @Autowired
    OllamaModerationService ollamaModerationService;

    public void updateDramaAuthStatus(DelegateExecution execution) {
        Map<String, Object> variables = execution.getVariables();
        log.info("审核短剧信息，准备更新数据库：{}",variables);

        //camunda 把他的收到的审核数据发给内容服务，把内容审核更新
        DramaAuthCompleteDTO dto = buildCompleteDTO(variables);
        contentServiceFeignClient.updateDramaAuthStatus(dto);
    }


    private DramaAuthCompleteDTO buildCompleteDTO(Map<String, Object> vars){
        DramaAuthCompleteDTO dto = new DramaAuthCompleteDTO();
        dto.setDramaId((Long) vars.get("dramaId"));
        if(vars.get("auditReason")!=null){
            dto.setAuditReason(vars.get("auditReason").toString());
        }

        if(vars.get("auditStatus")!=null){
            dto.setAuditStatus(vars.get("auditStatus").toString());
        }

        if(vars.get("approve")!=null){
            dto.setApprove((Boolean) vars.get("approve"));
        }

        if(vars.get("authName")!=null){
            dto.setAuthName(vars.get("authName").toString());
        }

        if (vars.get("authStatus")!=null){
            dto.setAuthStatus(vars.get("authStatus").toString());
        }

        return dto;
    }

    public void aiCheck(DelegateExecution execution) {
        //TODO 1、审核的短剧的id、简介
        Map<String, Object> variables = execution.getVariables();
        Object dramaId = variables.get("dramaId");
        Object dramaName = variables.get("dramaName");
        Object description = variables.get("description");
        log.info("AI内容审核启动，正在审核短剧：{},{},{}",dramaId,dramaName,description);

        if(!StringUtils.isEmpty(dramaName.toString())){
            String result = ollamaModerationService.moderation(dramaName.toString());
            variables.put("authName",result);
        }

        //抛出异常自动任务就认为炸了
        if(!StringUtils.isEmpty(description.toString())){
            String result = ollamaModerationService.moderation(description.toString());
            variables.put("authDescription",result);
        }

        //TODO 数据 重新set进去，自动传递;
        //TODO 自动任务（Service Task） 调用 setVariables
        execution.setVariables(variables);

        //TODO 手动任务（User Task）。完成的时候往下传
        // taskService.complete(task.getId(),variables);
        //获取当前任务
//                Task task = taskService.createTaskQuery()
//                        .processInstanceId(execution.getProcessInstanceId())
//                        .singleResult();
//                taskService.complete(task.getId(),variables);
    }

    public void audiService(DelegateExecution execution) {
        log.info("audiService 机器自动审核");
        Map<String, Object> variables = execution.getVariables();
        System.out.println("variables: " + variables);

//            Thread.sleep(1000*60);
        String instanceId = execution.getProcessInstanceId();
        System.out.println("审核完成...");
        List<Task> tasks = taskService.createTaskQuery()
            .processInstanceId(instanceId)
            .list();
        System.out.println("当前流程的所有任务：tasks: " + tasks);

        //结束任务
//            taskService.complete(task.getId());
    }


    /**
     * 腾讯云转码
     * @param execution
     */
    public void tecentVodTranslator(DelegateExecution execution) {


        Map<String, Object> variables = execution.getVariables();
        log.info("审核短剧信息，准备更新数据库：{}",variables);
        log.info("腾讯云转码正在执行....");

        //camunda 把他的收到的审核数据发给内容服务，把内容审核更新
        DramaAuthCompleteDTO dto = buildCompleteDTO(variables);
        //远程调用内容转码服务
        contentServiceFeignClient.tencentVodTransalator(dto);

    }

    /**
     * rag数据入库
     * @param execution
     */
    public void ragDataHandler(DelegateExecution execution) {
        log.info("rag数据入库正在执行....");
    }
}
